$(function () {
    var unitName = getQueryString("unitName");
    if (unitName == null) {
        unitName = '';
	}

    $("#jqGrid").jqGrid({
        url: '../customvisit/list',
        datatype: "json",
        postData: {"unit_name":unitName},
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden: true},
			{ label: '创建者', name: 'createBy', index: 'create_by', width: 80,hidden: true },
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 80,hidden: true },
			{ label: '更新者', name: 'updateBy', index: 'update_by', width: 80,hidden: true },
			{ label: '更新时间', name: 'updateDate', index: 'update_date', width: 80,hidden: true },
            { label: '单位名称', name: 'unitName', index: 'unit_name', width: 80 },
            { label: '单位编码', name: 'unitId', index: 'unit_id', width: 80,hidden: true },
			{ label: '企业联系人', name: 'contactName', index: 'contact_name', width: 80 },
			{ label: '联系人岗位', name: 'contactJob', index: 'contact_job', width: 80 }, 			
			{ label: '拜访形式', name: 'visitForm', index: 'visit_form', width: 80 ,formatter: getDictInfo},
			{ label: '拜访地点', name: 'visitAddress', index: 'visit_address', width: 80 }, 			
			{ label: '拜访时间', name: 'visitTime', index: 'visit_time', width: 80,formatter:"date", formatoptions: {newformat:'Y-m-d'}},
			{ label: '联系方式', name: 'visitTel', index: 'visit_tel', width: 80 }, 			
			{ label: '拜访内容', name: 'visitContent', index: 'visit_content', width: 80 }, 			
            { label: '备注信息', name: 'remarks', index: 'remarks', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

//获取url带的参数
function getQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null) {
        return  decodeURI(r[2]);
	}
    return null;
}

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            unitName: null,
            visit_style:null,
			startTime:null,
			endTime:null
        },
		showList: true,
		title: null,
		customVisit: {},
        visitStyleList:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.customVisit = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getListinfo();
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            vm.customVisit.visitTime = $("#visitTime").val();
			var url = vm.customVisit.id == null ? "../customvisit/save" : "../customvisit/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.customVisit),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../customvisit/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../customvisit/info/"+id, function(r){
                vm.customVisit = r.customVisit;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'unit_name': vm.q.unitName,
					      'visit_style':vm.q.visit_style,
				  	 	  'startTime':$("#startTime").val(),
						  'endTime':$("#endTime").val() },
                page:page
            }).trigger("reloadGrid");
		},
        getListinfo: function(){
            $.get("../custombaseinfo/getListinfo", function(r){
                vm.visitStyleList = r.visit_style;
            });
        }

	}
});